package dli_and_obs.job_utils

import java.util.{List, Properties}

import com.huawei.dli.sdk.{DLIClient, PackageResource}
import dli_and_obs.utils
/**
  * @Description TODO 
  * @Author liutaowei
  * @Date 2019-5-22 15:23
  */
object dli_job_run {

  def DLI_job(properties:Properties):String={
    val dli_client = utils.dli_utils.get_dli_client(properties) // 获取client
    //    val check_cluster = utils.cluster_utils.check_dli_cluster(dli_client,properties) // 检查是否创建队列
    //    properties.store(new FileWriter(prop_path),"") // 将改动的properties保存

    val resource_list = get_resource_list(properties,dli_client) // 过去资源列表
    val run_job = dli_job_run(resource_list,properties) // 开始任务

    return "success"
  }
  /**
  *@Author: Liutaowei
  *@Date:2019-5-21 16:15
  *@Description: 获取资源队列
  */
  private def get_resource_list(properties:Properties,dli_client:DLIClient):(List[PackageResource],DLIClient)={
    val resources = dli_client.listAllResources
    val package_name = properties.getProperty("package_name")

    import scala.collection.JavaConverters._
    val dli_resources = resources.getPackageResources
      .filter(_.getResourceName== package_name).toList.asJava


    if(dli_resources.size>0){
      return (dli_resources,dli_client)
    }else{
      println("未获取到资源,请查看包是否错误")
      return null
    }
  }

  /**
  *@Author: Liutaowei
  *@Date:2019-5-21 16:16
  *@Description: 判断是否有相关资源 并提交任务
  */
  private def dli_job_run(resource_list:(List[PackageResource],DLIClient),properties:Properties):String = {
    if(null == resource_list){
      return "fail"
    }else {
      val result = utils.dli_utils.batchJobs(resource_list,properties)
      return result
    }
  }
}
